using RugbyClubWpf.Helpers;
using RugbyClubWpf.Models;
using RugbyClubWpf.Data;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;
using Microsoft.EntityFrameworkCore;

namespace RugbyClubWpf.ViewModels
{
    /// <summary>
    /// ViewModel for the Login view.
    /// </summary>
    public class LoginViewModel : INotifyPropertyChanged
    {
        private string _username;
        public static string _password;
        private string _role;

        /// <summary>
        /// Gets or sets the username.
        /// </summary>
        public string Username
        {
            get => _username;
            set
            {
                _username = value;
                OnPropertyChanged();
            }
        }

        /// <summary>
        /// Gets or sets the password.
        /// </summary>
        public string Password
        {
            get => _password;
            set
            {
                _password = value;
                OnPropertyChanged();
            }
        }

        /// <summary>
        /// Gets or sets the role.
        /// </summary>
        public string Role
        {
            get => _role;
            set
            {
                _role = value;
                OnPropertyChanged();
            }
        }

        /// <summary>
        /// Gets or sets the collection of users.
        /// </summary>
        public ObservableCollection<User> Users { get; set; }

        /// <summary>
        /// Gets the login command.
        /// </summary>
        public ICommand LoginCommand { get; }

        /// <summary>
        /// Initializes a new instance of the <see cref="LoginViewModel"/> class.
        /// </summary>
        public LoginViewModel()
        {
            using (var context = new RugbyClubContext())
            {
                Users = new ObservableCollection<User>(context.Users.ToList());
            }

            LoginCommand = new RelayCommand(_ => Login());
        }

        private void Login()
        {
            Password = User.HashPassword(_password);
            var user = Users.FirstOrDefault(u => u.Username == Username && u.Password == Password);
            if (user != null)
            {
                Role = user.Role;
                MessageBox.Show($"Login successful! Role: {Role}");
                // Navigate to the appropriate view based on the role
                Application.Current.MainWindow.Hide();
                if (Role == "Coach")
                {
                    var coachView = new Views.CoachView
                    {
                        DataContext = new CoachViewModel(user.UserId)
                    };
                    coachView.Show();
                }
                else if (Role == "Player")
                {
                    var playerView = new Views.PlayerView
                    {
                        DataContext = new PlayerViewModel(user.UserId)
                    };
                    playerView.Show();
                }
                else if (Role == "President")
                {
                    var presidentView = new Views.PresidentView
                    {
                        DataContext = new PresidentViewModel()
                    };
                    presidentView.Show();
                }
            }
            else
            {
                MessageBox.Show("Invalid username or password");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
    }
}
